﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;//调用外部的DLL
using System.Diagnostics;
using System.IO;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private int mouseX = 0;
        private int mouseY = 0;
        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);

        [DllImport("msvcrt.dll", SetLastError = false, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private extern static void system(string command); // longjmp
        public Form1()
        {
            InitializeComponent();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 23");
            capturePic();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 4");
            capturePic();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 19");
            capturePic();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 21");
            //System.Threading.Thread.Sleep(1000);
            capturePic();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 22");
            //System.Threading.Thread.Sleep(1000);
            capturePic();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input keyevent 20");
            //System.Threading.Thread.Sleep(1000);
            capturePic();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            WinExec("adb kill-server", 0);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            WinExec("adb start-server", 0);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            WinExec("adb shell am start -n com.sian.monitor/.SplashActivity", 0);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            WinExec("adb shell input keyevent 176", 0);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //WinExec("adb shell input keyevent 82", 0);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            getBackString("adb shell input text "+textBox1.Text);
            capturePic();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button13_Click(object sender, EventArgs e)
        {
            WinExec("adb shell am start -n com.sian.control/.MainActivity", 0);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            WinExec("adb shell input keyevent 187", 0);
        }

        private void button15_Click(object sender, EventArgs e)
        {
             
            label1.Text = getBackString("adb devices");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        private string getBackString(string commond) {
            Process CmdProcess = new Process();
            CmdProcess.StartInfo.FileName = "cmd.exe";
            CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口    
            CmdProcess.StartInfo.UseShellExecute = false;       //不启用shell启动进程  
            CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入    
            CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
            CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
            CmdProcess.StartInfo.Arguments = "/c " + commond;//“/C”表示执行完命令后马上退出  
            CmdProcess.Start();//执行  

            //CmdProcess.StandardOutput.ReadToEnd();//获取返回值 
            StreamReader sr = CmdProcess.StandardOutput;//获取返回值 
            string line = "";
            string result = "";
            int num = 1;
            while ((line = sr.ReadLine()) != null)
            {
                if (line != "")
                {
                    result += line + "\n";
                    Console.WriteLine(line + " " + num++);
                }
            }
            CmdProcess.WaitForExit();//等待程序执行完退出进程  

            CmdProcess.Close();//结束 

            return result;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            WinExec("adb shell svc wifi enable", 0);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            WinExec("adb shell svc wifi disable", 0);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            WinExec("adb reboot", 0);
        }
        private string pathname = string.Empty;     		//定义路径名变量
        private void button19_Click(object sender, EventArgs e)
        {
            capturePic();
        }
        private void capturePic() {
            if (this.pictureBox1.Image != null) {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;//
                if (System.IO.File.Exists(@"./sc.png"))
                {
                    // Use a try block to catch IOExceptions, to
                    // handle the case of the file already being
                    // opened by another process.
                    try
                    {
                        System.IO.File.Delete(@"./sc.png");
                    }
                    catch (System.IO.IOException e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                    }
                }
            }
            getBackString("adb shell screencap -p /sdcard/sc.png");
            getBackString("adb pull /sdcard/sc.png");
            this.pictureBox1.Image = Image.FromFile("./sc.png");
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Point screenPoint = pictureBox1.PointToClient(Control.MousePosition);
            label1.Text = "X="+screenPoint.X.ToString()+" Y="+ screenPoint.Y.ToString();
            getBackString("adb shell input tap " + screenPoint.X.ToString() + " " + screenPoint.Y.ToString());
            System.Threading.Thread.Sleep(1000);
            capturePic();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            capturePic();
        }


        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            /*if (Math.Abs(e.X - mouseX) > 10 || Math.Abs(e.Y - mouseY) > 10)
            {
                WinExec("adb shell input swipe " + mouseX + " " + mouseY + " " + e.X + " " + e.Y, 0);
            }*/

        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseX = e.Y;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                /*string[] names = fileDialog.FileNames;

                foreach (string file in names)
                {
                    MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }*/
                string filename = fileDialog.FileName;
                getBackString("adb push "+filename+" "+"/sdcard/"+filename.Substring(filename.LastIndexOf("\\")+1,filename.Length- filename.LastIndexOf("\\")-1));
                label1.Text = getBackString("adb shell ls /sdcard/");
            }

        }
    }
}
